package com.example.object.game;

import android.content.Context;
import android.util.Log;

import com.example.bounce.R;
import com.example.globject.GLSquare;
import com.example.object.GameShape;

import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

/**
 * Created by Lucas on 03/07/13.
 */
public class Wooden extends GameShape{
    /**
     * The glObject of the Wooden
     */
    private GLSquare _glSquare;
    /**
     * The JBox2D BodyDef of the Wooden
     */
    private BodyDef _bodyDef;
    /**
     * The JBox2D Body of the Wooden
     */
    private Body _body;
    /**
     * The width and height of the object
     */
    private float _width, _height;
    /**
     * The debug TAG
     */
    private final String TAG = "SignShape";

    /**
     * The public constructor
     * @param context the context of the application
     * @param xCenter the coordinate x of the origin point
     * @param yCenter the coordinate y of the origin point
     * @param midHeight the height of the Wooden
     * @param midWidth the width of the wooden
     */
    public Wooden(Context context, float xCenter, float yCenter,
                     float midHeight, float midWidth){

        Log.i(TAG, "constructor");
        _handlerInitiate = false;
        _width=midWidth;   _height=midHeight;
        _userData = 0;
        _glSquare = new GLSquare(context, xCenter, yCenter, midHeight, midWidth, R.raw.wooden);

        // create definition
        _bodyDef = new BodyDef();

        _bodyDef.position.set(new Vec2(xCenter, yCenter));

        _bodyDef.type = BodyType.STATIC;
    }

    /**
     * Add the JBox2D object to the JBox2D world of the game
     * @param world the JBox2D world
     */
    public void addToWorld(World world){
        float halfWidth = _width/2f;
        float halfHeight = _height/2f;
        // need to reverse the width and height for some reason
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(halfHeight, halfWidth*2f, new Vec2(halfHeight, 0), 0f);
        _body = world.createBody(_bodyDef);
        // body features like density, restitution, ....
        FixtureDef fd = new FixtureDef();
        fd.density = 1000f;
        fd.restitution = 0.0f;
        fd.friction = 0.0f;
        fd.shape = shape;
        _body.createFixture(fd);
    }

    /**
     * set the gl handler to draw the Wooden
     * @param positionHandle
     * @param textureHandle
     * @param MVPMatrixHandle
     * @param vMatrix
     * @param projMatrix
     */
    public void setGLHandler(int positionHandle, int textureHandle, int MVPMatrixHandle,
                             float vMatrix[], float projMatrix[]){
        _glSquare.setGLHandler(positionHandle, textureHandle, MVPMatrixHandle, vMatrix, projMatrix, null);
        _handlerInitiate = true;
    }

    /**
     * Handle the drawing function
     */
    @Override
    public void draw(){
        _glSquare.draw();
    }

    public String toString(){
        return "\n glSquare: " + _glSquare.toString();
    }

    public GLSquare getGLSquare(){
        return _glSquare;
    }
}
